﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ExcelUtilities.Data
{
    /// <summary>
    /// DataSet扩展函数
    /// </summary>
    public static class DataSetExtensions
    {
        /// <summary>
        /// DataSet转WorkbookData
        /// </summary>
        /// <param name="dataset">要转换的DataSet</param>
        /// <returns></returns>
        public static WorkbookData ToWorkbookData(this DataSet dataset)
        {
            WorkbookData workbookData = new WorkbookData();
            foreach (DataTable table in dataset.Tables)
            {
                var tableData = new TableData();
                tableData.Name = table.TableName;
                foreach (DataRow row in table.Rows)
                {
                    var rowData = new Dictionary<string, CellValue>();
                    foreach (DataColumn column in table.Columns)
                    {
                        rowData.Add(column.ColumnName, new CellValue(row[column.ColumnName]));
                    }
                    tableData.RowDatas.Add(rowData);
                }
                workbookData.SheetDatas.Add(new WorksheetData() { Name = table.TableName });
                workbookData.SheetDatas[0].TableDatas.Add(tableData);
            }

            return workbookData;
        }
        /// <summary>
        /// DataTable转WorkbookData
        /// </summary>
        /// <param name="dataset">要转换的DataTable</param>
        /// <returns></returns>
        public static WorkbookData ToWorkbookData(this DataTable datatable)
        {
            WorkbookData workbookData = new WorkbookData();
            var tableData = new TableData();
            tableData.Name = datatable.TableName;
            foreach (DataRow row in datatable.Rows)
            {
                var rowData = new Dictionary<string, CellValue>();
                foreach (DataColumn column in datatable.Columns)
                {
                    rowData.Add(column.ColumnName, new CellValue(row[column.ColumnName]));
                }
                tableData.RowDatas.Add(rowData);
            }
            workbookData.SheetDatas.Add(new WorksheetData() { Name = datatable.TableName });
            workbookData.SheetDatas[0].TableDatas.Add(tableData);

            return workbookData;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataset"></param>
        /// <param name="saveFile"></param>
        public static void ExportToExcel(this DataSet dataset, string saveFile)
        {
            var workbookData = ToWorkbookData(dataset);
            Cfg.Workbook workbookCfg = new Cfg.Workbook();
            for (int i = 0; i < dataset.Tables.Count; i++)
            {
                var tableName = dataset.Tables[i].TableName;
                Cfg.Worksheet worksheet = new Cfg.Worksheet() { Name = tableName, SheetName = tableName };
                Cfg.Range range = new Cfg.Range() { Name = tableName, StartRow = 2 };
                foreach (DataColumn column in dataset.Tables[i].Columns)
                {
                    var cellAddr = ExcelUtilities.Cfg.AddressExtensions.ToAddr(new Cfg.CellPosition() { Row = 1, Col = column.Ordinal + 1 });
                    worksheet.Cells.Add(new Cfg.Cell() { Name = column.ColumnName, Address = cellAddr });
                    workbookData.SheetDatas[i].CellDatas.Add(column.ColumnName, new CellValue(column.ColumnName));
                    range.Columns.Add(new Cfg.Column() { Name = column.ColumnName, Order = column.Ordinal + 1 });
                }
                worksheet.Ranges.Add(range);
                workbookCfg.Worksheets.Add(worksheet);
            }
            var excelWriter = ExcelFactory.GetExcelWriter();
            excelWriter.Write(workbookCfg, null, workbookData, saveFile);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        /// <param name="saveFile"></param>
        public static void ExportToExcel(this DataTable table, string saveFile)
        {
            var workbookData = ToWorkbookData(table);
            var tableName = table.TableName;
            Cfg.Workbook workbookCfg = new Cfg.Workbook();
            Cfg.Worksheet worksheet = new Cfg.Worksheet() { Name = tableName, SheetName = tableName };
            Cfg.Range range = new Cfg.Range() { Name = tableName, StartRow = 2 };
            foreach (DataColumn column in table.Columns)
            {
                var cellAddr = ExcelUtilities.Cfg.AddressExtensions.ToAddr(new Cfg.CellPosition() { Row = 1, Col = column.Ordinal + 1 });
                worksheet.Cells.Add(new Cfg.Cell() { Name = column.ColumnName, Address = cellAddr });
                workbookData.SheetDatas[0].CellDatas.Add(column.ColumnName, new CellValue(column.ColumnName));
                range.Columns.Add(new Cfg.Column() { Name = column.ColumnName, Order = column.Ordinal + 1 });
            }
            worksheet.Ranges.Add(range);
            workbookCfg.Worksheets.Add(worksheet);
            var excelWriter = ExcelFactory.GetExcelWriter();
            excelWriter.Write(workbookCfg, null, workbookData, saveFile);
        }
    }
}